package com.gxuwz.medical.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.user.User;
import com.gxuwz.medical.vo.PageBean;



public abstract class GenericDao<T> {

	protected abstract T handle(ResultSet rs)throws SQLException;

	public PageBean queryAll(String sql,Object[] params,int page,int size)throws SQLException{
		Connection conn = null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List pageData=new ArrayList<T>();
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement(sql+" limit "+(page-1)*size+","+size+"");
			int index=1;
			if(params!=null){
			  for(Object param:params){
				  if(param instanceof String){
					  pstmt.setString(index++, (String)param);
				  }
				  if(param instanceof Integer){
					  pstmt.setInt(index++, (Integer)param);
				  }
				  if(param instanceof Float){
					  pstmt.setFloat(index++, (Float)param);
				  }
			  }
			}
			rs=pstmt.executeQuery();
			while(rs.next()){
				T t=handle(rs);
				pageData.add(t);
			}
			PageBean pagebean = new PageBean(page,size,count(sql,params));
			pagebean.setPageData(pageData);
			return pagebean;
		}catch(SQLException e){
			throw new SQLException("查询全部记录失败！",e);
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
		
	}
	public List<T> queryAll(String sql,Object[] params)throws SQLException{
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<T> ret=new ArrayList<T>();
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement(sql);
			int index=1;
			if(params!=null){
			  for(Object param:params){
				  if(param instanceof String){
					  pstmt.setString(index++, (String)param);
				  }
				  if(param instanceof Integer){
					  pstmt.setInt(index++, (Integer)param);
				  }
				  if(param instanceof Float){
					  pstmt.setFloat(index++, (Float)param);
				  }
			  }
			}
			rs=pstmt.executeQuery();
			while(rs.next()){
				T t=handle(rs);
				ret.add(t);
			}
			return ret;
		}catch(SQLException e){
			throw new SQLException("查询全部记录失败！",e);
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}	
	}
	public int count(String sql,Object[] params)throws SQLException{
		int count = 0;
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement("select count(*) from ("+sql+") as temp");
			int index=1;
			if(params!=null){
				  for(Object param:params){
					  if(param instanceof String){
						  pstmt.setString(index++, (String)param);
					  }
					  if(param instanceof Integer){
						  pstmt.setInt(index++, (Integer)param);
					  }
					  if(param instanceof Float){
						  pstmt.setFloat(index++, (Float)param);
					  }
				  }
				}
			rs=pstmt.executeQuery();
			if(rs.next()){
				 count=rs.getInt(1);
			}
			return count;
		}catch(SQLException e){
			throw new SQLException("查询记录数失败！",e);
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
}
